<?php
declare (strict_types = 1);

namespace app\webadmin\controller;

use app\webadmin\model\Cesuan;
use app\webadmin\BaseController;
use app\webadmin\model\Users as UsersList;
use think\facade\Db;
use think\facade\View;
use \think\facade\Log;
use think\exception\ValidateException;
use app\common\Redis;
class Users extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['id|nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }

            if (!empty($param['type'])) {

               $where[] = ['type', '=', $param['type']];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = UsersList::where($where)
                ->field("id,nickname,mobile,openid,type,createip,last_login_time,status,login_num,create_time")
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])->each(function ($item, $k) {
                    $item['usertype']=getUserType($item['type']);
                    $item['createtime']=date('Y-m-d H:i:s',$item['create_time']);
                    return $item;
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

    public function cslist(){
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['id|codes|name|nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = Cesuan::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])->each(function ($item, $k) {

                    $item['createtime']=date('Y-m-d H:i:s',$item['create_time']);
                    return $item;
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

    public function csinfo(){
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        if ($id > 0) {
            $datas = Cesuan::where(['id' => $id])->find();
            if($datas){
                if($datas['datas']<0.00 && $datas['datas2']<0.00){

                    $buymoney=$datas['datas']+$datas['datas2'];
                }else if($datas['datas']<0.00 && $datas['datas2']>=0.00){

                    $buymoney=$datas['datas'];
                }else if($datas['datas']>=0.00 && $datas['datas2']<0.00){

                    $buymoney=$datas['datas2'];
                }else{
                    $buymoney=0.00;
                }
                if($datas['zt']==1 || $datas['zt2']==1){
                    $buyzt=1;
                }else{
                    $buyzt=0;
                }
                $datas['buyzt']=$buyzt;
                $datas['heji']=$buymoney;
                View::assign('data', $datas);
            }
            View::assign('id', $id);
            return view();
        }
    }

    /*public function buyslist(){
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['id|codes|name|nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = Buys::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])->each(function ($item, $k) {

                    $item['createtime']=date('Y-m-d H:i:s',$item['create_time']);
                    return $item;
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

    public function buyinfo(){
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        if ($id > 0) {
            $datas = Buys::where(['id' => $id])->find();
            if($datas){
                if($datas['datas']<0.00 && $datas['nodatas']<0.00){

                    $buymoney=$datas['datas']+$datas['nodatas'];
                }else if($datas['datas']<0.00 && $datas['nodatas']>=0.00){

                    $buymoney=$datas['datas'];
                }else if($datas['datas']>=0.00 && $datas['nodatas']<0.00){

                    $buymoney=$datas['nodatas'];
                }else{
                    $buymoney=0.00;
                }
                if($datas['zt']==1 || $datas['nozt']==1){
                    $buyzt=1;
                }else{
                    $buyzt=0;
                }
                $datas['buyzt']=$buyzt;
                $datas['heji']=$buymoney;
                View::assign('data', $datas);
            }
            View::assign('id', $id);
            return view();
        }
    }

    public function selllist(){
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['id|codes|name|nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = Sells::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])->each(function ($item, $k) {

                    $item['createtime']=date('Y-m-d H:i:s',$item['create_time']);
                    return $item;
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }
    public function sellsinfo(){
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        if ($id > 0) {
            $datas = Sells::where(['id' => $id])->find();
            if($datas){
                if($datas['zt']==1 || $datas['nozt']==1){
                    $buyzt=1;
                }else{
                    $buyzt=0;
                }
                if($datas['datas']<0 && $datas['nodatas']<0){
                    $buymoney=$datas['datas']+$datas['nodatas'];
                }else if($datas['datas']<0 && $datas['nodatas']>=0){
                    $buymoney=$datas['datas'];
                }else if($datas['datas']>=0 && $datas['nodatas']<0){
                    $buymoney=$datas['nodatas'];
                }else{
                    $buymoney=0;
                }
                $datas['heji']=$buymoney;
                $datas['buyzt']=$buyzt;
                View::assign('data', $datas);
            }
            View::assign('id', $id);
            return view();
        }
    }*/
}